package ejb;

import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import entityJPA.Candidate;
import entityJPA.User;
import entityJPA.Usertype;

/**
 * @generated DT_ID=none
 */
@Stateless(name = "UserEJB", mappedName = "jt-hr-EAR-jt-hr-EE-UserEJB")
public class UserEJB implements UserEJBLocal, UserEJBRemote {

	/**
	 * @generated DT_ID=none
	 */
	@Resource
	SessionContext sessionContext;

	/**
	 * @generated DT_ID=none
	 */
	@PersistenceContext(unitName = "JTResource")
	private EntityManager em;

	/**
	 * @generated DT_ID=none
	 */
	public UserEJB() {

	}

	/**
	 * @generated DT_ID=none
	 */
	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public Object queryByRange(String jpqlStmt, int firstResult, int maxResults) {
		Query query = em.createQuery(jpqlStmt);
		if (firstResult > 0) {
			query = query.setFirstResult(firstResult);
		}
		if (maxResults > 0) {
			query = query.setMaxResults(maxResults);
		}

		return query.getResultList();
	}

	/**
	 * @generated DT_ID=none
	 */
	public User persistUser(User user) {
		em.persist(user);
		return user;
	}

	/**
	 * @generated DT_ID=none
	 */
	public User mergeUser(User user) {
		// IllegalArgumentException
		// TransactionRequiredException
		return em.merge(user);
	}

	/**
	 * @generated DT_ID=none IllegalArgumentException - if the first argument
	 *            does not denote an entity type or the second argument is not a
	 *            valid type for that entity's primary key or is null
	 *            TransactionRequiredException - if there is no transaction and
	 *            a lock mode other than NONE is specified
	 *            OptimisticLockException - if the optimistic version check
	 *            fails PessimisticLockException - if pessimistic locking fails
	 *            and the transaction is rolled back LockTimeoutException - if
	 *            pessimistic locking fails and only the statement is rolled
	 *            back PersistenceException - if an unsupported lock call is
	 *            made
	 */
	public void removeUser(User user) {
		user = em.find(User.class, user.getIdUser());
		em.remove(user);
	}

	/**
	 * @generated DT_ID=none
	 */
	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<User> getUserFindAll() {
		return em.createNamedQuery("User.findAll").getResultList();
	}
	
	
	public List<User> getUserLimit(int startIndex, int endIndex)
			throws Exception {
		Query q = em.createNamedQuery("User.findAllActive");
		q.setFirstResult(startIndex);
		q.setMaxResults(endIndex);

		return q.getResultList();
	}



	/**
	 * @generated DT_ID=none
	 */
	public Usertype persistUsertype(Usertype usertype) {
		em.persist(usertype);
		return usertype;
	}

	/**
	 * @generated DT_ID=none
	 */
	public Usertype mergeUsertype(Usertype usertype) {
		return em.merge(usertype);
	}

	/**
	 * @generated DT_ID=none
	 */
	public void removeUsertype(Usertype usertype) {
		usertype = em.find(Usertype.class, usertype.getIdUserType());
		em.remove(usertype);
	}

	/**
	 * @generated DT_ID=none
	 */
	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<Usertype> getUsertypeFindAll() {
		return em.createNamedQuery("Usertype.findAll").getResultList();
	}

	public User findByUserName(String username) {
		Query query = em
				.createQuery("SELECT u FROM User u where u.username = :us");
		query.setParameter("us", username);
		if (query.getResultList().size() == 0)
			return null;
		return (User) query.getResultList().get(0);
	}

	public User findByUserNameAndPassword(String userName, String password) {

		Query query = em
				.createQuery("SELECT u FROM User u where u.username = :us and u.password = :pass");
		query.setParameter("us", userName);
		query.setParameter("pass", password);

		User u = (User) query.getSingleResult();

		return u;

	}

	public Usertype findByTitle(String title) {
		Query query = em
				.createQuery("SELECT u FROM Usertype u where u.title = :t");
		query.setParameter("t", title);

		Usertype ut = (Usertype) query.getSingleResult();

		return ut;
	}

	public String generateUserName(User user) {
		String username = null;
		int contor = 0;
		if (user.getLastName().length() <= 5 && user.getUsername() == null) {
			user.setUsername(user.getLastName() + user.getFirstName());
			user.setUsername(user.getUsername().substring(0, 6));
		} else
			user.setUsername(user.getLastName().substring(0, 5)
					+ user.getFirstName().charAt(contor));
		contor++;
		while (findByUserName(user.getUsername()) != null) {
			user.setUsername(user.getUsername()
					+ user.getFirstName().charAt(contor));
			contor++;
		}
		username = user.getUsername();
		return username;
	}

	public User getUserById(int id) {
		User u = em.find(User.class, id);
		return u;
	}

	public List<User> filterByFirstName(String firstName) {
		Query q = em.createNamedQuery("User.filterByFirstName");

		q.setParameter("fn", firstName + "%");
		return q.getResultList();
	}

	@Override
	public List<User> filterByLastName(String lastName) {
		// TODO Auto-generated method stub

		Query q = em.createNamedQuery("User.filterByLastName");
		q.setParameter("ln", lastName + "%");
		return q.getResultList();
	}



}
